Reddit API

This notebook was planned to get comments data from some user on Reddit. Yet, it is not able to proceed with request without providing an agent name (as the default wrapper is overwhelmed). So we defined it and put it into a dictionary called headers as an argument to requests.get function. The Reddit API JSON response can be publicly accessed as presented on the 2nd code line.


In [1]:
import requests

In [2]:
data = requests.get(r'http://www.reddit.com/user/narek/comments/.json',headers = {'User-agent': 'Educational bot'})

In [4]:
data = data.json()

In [5]:
from pprint import pprint
pprint(data)


{u'data': {u'after': None,
           u'before': None,
           u'children': [{u'data': {u'approved_by': None,
                                    u'archived': True,
                                    u'author': u'narek',
                                    u'author_flair_css_class': None,
                                    u'author_flair_text': None,
                                    u'banned_by': None,
                                    u'body': u'messi',
                                    u'body_html': u'<div class="md"><p>messi</p>\n</div>',
                                    u'can_gild': True,
                                    u'controversiality': 0,
                                    u'created': 1268439679.0,
                                    u'created_utc': 1268410879.0,
                                    u'distinguished': None,
                                    u'downs': 0,
                                    u'edited': False,
                                    u'gilded': 0,
                                    u'id': u'c0m2phy',
                                    u'likes': None,
                                    u'link_author': u'jhirorasta',
                                    u'link_id': u't3_b1ng1',
                                    u'link_permalink': u'https://www.reddit.com/r/reddit.com/comments/b1ng1/share_your_favourite_travel_photos_with_lonely/',
                                    u'link_title': u'Share your favourite travel photos with Lonely Planet and win over $170,000 in prizes! | home',
                                    u'link_url': u'http://www.lonelyplanet.com/win/?affil=100google',
                                    u'mod_reports': [],
                                    u'name': u't1_c0m2phy',
                                    u'num_comments': 3,
                                    u'num_reports': None,
                                    u'over_18': False,
                                    u'parent_id': u't3_b1ng1',
                                    u'quarantine': False,
                                    u'removal_reason': None,
                                    u'replies': u'',
                                    u'report_reasons': None,
                                    u'saved': False,
                                    u'score': 1,
                                    u'score_hidden': False,
                                    u'stickied': False,
                                    u'subreddit': u'reddit.com',
                                    u'subreddit_id': u't5_6',
                                    u'subreddit_name_prefixed': u'r/reddit.com',
                                    u'subreddit_type': u'archived',
                                    u'ups': 1,
                                    u'user_reports': []},
                          u'kind': u't1'}],
           u'modhash': u''},
 u'kind': u'Listing'}

In [6]:
for child in data['data']['children']:
    print child['data']['id'], " ", child['data']['author'],child['data']['body']
    print


c0m2phy   narek messi